package com.neusoft.elmboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.elmboot.Dto.UserDto;
import com.neusoft.elmboot.common.constant.RoleAndPermisserionConstant;
import com.neusoft.elmboot.common.exception.LoginFailedException;
import com.neusoft.elmboot.mapper.RolePermissionMapper;
import com.neusoft.elmboot.mapper.UserMapper;
import com.neusoft.elmboot.mapper.UserRoleMapper;
import com.neusoft.elmboot.po.RolePermission;
import com.neusoft.elmboot.po.UserEntity;
import com.neusoft.elmboot.po.UserRole;
import com.neusoft.elmboot.service.RolePermissionService;
import com.neusoft.elmboot.service.UserRoleService;
import com.neusoft.elmboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("myUserDetailsService")
public class MyUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.info("------------loadUserByUsername------------",userName);
        //登录认证逻辑
        /*LambdaQueryWrapper<UserEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userName!=null,UserEntity::getUserName,userName);
        UserEntity userEntity = userService.getOne(lqw);*/
        UserDto userDto = new UserDto();
        userDto.setUserName(userName);
        UserEntity userEntity = userService.getByUserName(userDto);

        if (userEntity==null){
            throw new LoginFailedException("登录失败，请检查用户名或密码");
        }

        //查找用户权限
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        LambdaQueryWrapper<UserRole> urLqw = new LambdaQueryWrapper<>();
        urLqw.eq(UserRole::getUserId,userEntity.getId());

        List<UserRole> userRoleList = userRoleService.getBaseMapper().selectList(urLqw);
        for (UserRole userRole : userRoleList) {
            if (RoleAndPermisserionConstant.SUPER_ADMIN_ROLE_ID==userRole.getRoleId()){
                grantedAuthorityList.add(new SimpleGrantedAuthority("*"));
                break;
            }

            List<String> permApis = rolePermissionMapper.selectPermApiByRoleId(userRole.getRoleId());
            for (String permApi : permApis) {
                grantedAuthorityList.add(new SimpleGrantedAuthority(permApi));
            }



        }

        userEntity.setAuthorities(grantedAuthorityList);
        return userEntity;

    }
}
